#include "login_mannager.h"

void drawAddAccount()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                  Login Mannage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|                         Add Acoount                      |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|   \033[01;32mNew User name   \033[01;35m|   \033[01;32mNew Password   \033[01;35m|    \033[01;32mNew User ID    \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|:                  |:                 |:                  |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue                                   |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);

}

void drawDeleteAccount()
{
    system("clear");
    std::cout << "\033[01;35m------------------------------------------------------------" << std::endl;
    std::cout << "|                  Login Mannage System                    |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|                      Delete Acoount                      |" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|\033[01;32mUser ID:                                                  \033[01;35m|" << std::endl;
    std::cout << "------------------------------------------------------------" << std::endl;
    std::cout << "|Press Enter To Continue...                                |" << std::endl;
    std::cout << "------------------------------------------------------------\033[0m" << std::endl;
    moveCursor('u', 4);
}

int addAccount()
{
    drawAddAccount();
    moveCursor('r',2);
    std::string new_username, new_password, new_userid;
    std::cout << "\033[01;32m";
    new_username = readCommand();
    if(new_username == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',22);
    char *p = getpass("");
    for(int i = 0; i < strlen(p); i++)
    {
        new_password.append(1, p[i]);
    }
    if(new_password == "q") return -1;
    moveCursor('u', 1);
    moveCursor('r',41);
    new_userid = readCommand();
    if(new_userid == "q") return -1;

    std::cout << "\033[0m";
    std::cout << new_username << " " << new_password << " " << new_userid << std::endl;
    if(new_username == "" || new_password == "" || new_userid == "") return 0;
    if(Login_isValid(new_username, new_userid))
    {
        std::fstream file;
        file.open(LG_PATH, std::ios::out|std::ios::app);
        std::string addline;
        addline.append("\"");
        addline.append(new_username);
        addline.append("\" \"");
        addline.append(new_password);
        addline.append("\" \"");
        addline.append(new_userid);
        addline.append("\"");
        file << addline << std::endl;
        return 1;
    }
    else return 0;
}

std::string deleteAccount()
{
    bool isok = 0;
    drawDeleteAccount();
    moveCursor('r', 9);
    std::cout << "\033[01;32m";
    std::string userid;
    userid = readCommand();
    if(userid == "Administrator") return userid;
    if(userid == "q") return "q";
    std::cout << "\033[0m";
    std::fstream in, out;
    in.open(LG_PATH);
    std::vector<std::string> ret;
    while(!in.eof())
    {
        std::vector<std::string> temp;
        std::string line;
        getline(in, line);
        if(line.empty()) continue;
        temp = split(line, ' ');
        if(temp[2] == userid)
        {
            isok = 1;
        }
        else ret.push_back(line);
    }
    if(isok)
    {
        out.open(LG_PATH, std::ios::out);
        for(auto a : ret) out << a << std::endl;
        return userid;
    }
    return "";
}

bool Login_isValid(std::string username, std::string userid)
{
    std::fstream file;
    std::string temp;
    file.open(LG_PATH, std::ios::in);
    while(!file.eof()) 
    {
        getline(file, temp);
        std::vector<std::string> lines = split(temp, ' ');
        if(lines[0] == username || lines[2] == userid) return 0;
    }
    return 1;
}
